import { _decorator, Component, instantiate, macro, math, Node, Prefab } from 'cc';
import { GameCtrl } from './GameCtrl';
const { ccclass, property } = _decorator;

@ccclass('PipeCtrl')
export class PipeCtrl extends Component {
    @property(Prefab) pipePrefab: Prefab

    private createRate: number = 2

    onLoad() {
        this.createRate = GameCtrl.instance.pipeRate
        this.schedule(this.createPipe, this.createRate, macro.REPEAT_FOREVER, 0.5)
    }

    createPipe() {
        const pipe = instantiate(this.pipePrefab)
        const random = math.randomRange(-100, 200)
        pipe.setPosition(pipe.position.x, random)
        this.node.addChild(pipe)
    }

    onDestroy() {
        this.unschedule(this.createPipe)
    }
}


